package Service;

import DAO.DealDAO;
import Utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class DealService
{
    @Autowired
    private DealDAO dealDAO;

    public Boolean Add_Buy(Buy buy)
    {
        try{
            buy.setBuyId(String.valueOf(System.currentTimeMillis()+(int)(Math.random()*99999)));
            dealDAO.Add_Buy(buy);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
    public Boolean Add_Sell(Sell sell)
    {
        try{
            sell.setSellId(String.valueOf(System.currentTimeMillis()+(int)(Math.random()*99999)));
            dealDAO.Add_Sell(sell);
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
    public ArrayList<Deal> GetAllDealByUserId(String UserId)
    {
        ArrayList<ArrayList> rawDealArraylist = null;
        ArrayList<Deal> dealArrayList = new ArrayList<Deal>();
        try{
            rawDealArraylist = dealDAO.Get_AllDeal(UserId);
            ArrayList<Buy> buyArrayList = rawDealArraylist.get(0);
            ArrayList<Sell> sellArrayList = rawDealArraylist.get(1);
            for(Buy buy: buyArrayList)
            {
                dealArrayList.add(new Deal("买入",buy.getStockCode(),buy.getBuyCount(),buy.getDealTime()!=null?"交易达成":"交易未达成"));

            }
            for(Sell sell: sellArrayList)
            {
                dealArrayList.add(new Deal("卖出",sell.getStockCode(),sell.getSellCount(),sell.getDealTime()!=null?"交易达成":"交易未达成"));
            }
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        finally {
            return dealArrayList;
        }
    }
}
